﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using Universe.OneC.DomainModel;

namespace Universe.OneC.ConsoleSample
{
    class Dump1CIO
    {
        private TextWriter writer;

        public Dump1CIO(TextWriter writer)
        {
            this.writer = writer;
        }

        public void WriteHtml(MetaClass meta, Dump1C resultSet)
        {
            string title =
                string.IsNullOrEmpty(meta.Synonym)
                    ? meta.Name
                    : meta.Synonym;

            writer.WriteLine("<Div class='classTitle'><span style='color:#909090'>Документы: </span>{0}</Div>", title);
            writer.WriteLine("<table class='classTable' border=0 cellspacing=0 cellpadding=0><tr>");

            List<MetaProperty> properties = meta.GetProperties();
            foreach (MetaProperty property in properties)
            {
                writer.WriteLine(
                    " <th>{0}</th>",
                    HttpUtility.HtmlEncode(string.IsNullOrEmpty(property.Synonym) ? property.Name : property.Synonym));
            }

            writer.Write("</tr>");

            List<MetaClass> parts = meta.Model.GetTableParts(meta);

            foreach (Row1C row in (IEnumerable)resultSet.ResultSet)
            {
                writer.WriteLine("<tr>");
                foreach (MetaProperty property in (IEnumerable<MetaProperty>)properties)
                {
                    PropertyValue pv = row.Cells.Dictionary[property.Name];
                    writer.WriteLine(" <td>{0}</td>", ValueAsHtml(pv));
                }

                writer.WriteLine("</tr>");

                foreach (MetaClass part in parts)
                {
                    PropertyOfTablePart ptp = row.Cells.Dictionary[part.Name] as PropertyOfTablePart;
                    if (ptp != null && ptp.Rows.Count > 0)
                    {
                        writer.WriteLine("<tr><td colspan={0}>", properties.Count);
                        writer.WriteLine("<table border=0 class='partTable' cellspacing=0 cellpadding=0><tr></td>");
                        writer.WriteLine("<Div class='partTitle'><span style='color:#909090'>Табличная часть: </span>{0}</Div>", string.IsNullOrEmpty(part.Synonym) ? part.Name : part.Synonym);

                        List<MetaProperty> partProperties = part.GetProperties();
                        writer.WriteLine("<tr>");
                        foreach (MetaProperty property in partProperties)
                        {
                            writer.WriteLine(
                                " <th>{0}</th>",
                                HttpUtility.HtmlEncode(string.IsNullOrEmpty(property.Synonym) ? property.Name : property.Synonym));
                        }

                        writer.WriteLine("</tr>");

                        foreach (Row1C rowPart in ptp.Rows)
                        {
                            writer.WriteLine("<tr>");
                            foreach (MetaProperty property in partProperties)
                            {
                                PropertyValue pv = rowPart.Cells.Dictionary[property.Name];

                                writer.WriteLine(" <td>{0}</td>", ValueAsHtml(pv));
                            }

                            writer.WriteLine("</tr>");


                        }
                        writer.WriteLine("</table></td></tr>");
                    }

                }



            }
        }

        static string ValueAsHtml(PropertyValue value)
        {
            string s1 = "<span style='color: #A0A0A0'>" + HttpUtility.HtmlEncode(value.TypeToDump()) + "</span>";
            string s2 = HttpUtility.HtmlEncode(value.ValueToDump());

            return (s1 + " " + s2).Trim();
        }

        public static readonly string Styles =
            @"
<Style>
 .classTitle { font-family: Verdana Helvetica Arial; font-size: 14pt; padding: 5 0 5 0}
 .partTitle { text-align: left; font-family: Verdana Helvetica Arial; font-size: 13pt; padding: 0 0 5 0}

.classTable {}
.partTable { margin-left: 30 }

.classTable th { font-family: Arial; font-size: 11pt; text-align: center; padding: 4 9 4 9; border: 1 solid #808080 }
.partTable th { font-family: Arial; font-size: 10pt; text-align: center; padding: 4 9 4 9; border: 1 solid #808080 }

.classTable td { font-family: Arial; font-size: 11pt; text-align: left; padding: 5 1 1 1; border: 1 solid #808080 }
.partTable td { font-family: Arial; font-size: 9pt; text-align: left; padding: 1 1 1 1; border: 1 solid #808080 }
</Style>
";
    }
}
